package com.woniuxy.classdeans.service.impl;

import com.woniuxy.classdeans.mapper.TeamInfoMapper;
import com.woniuxy.classdeans.mapper.TeamMasterMapper;
import com.woniuxy.classdeans.service.TeamInfoService;
import com.woniuxy.commons.entity.wyh.TeamCondition;
import com.woniuxy.commons.entity.wyh.TeamInfo;
import com.woniuxy.commons.entity.wyh.TeamMaster;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author wangyanhui
 * @date 2020/7/17 - 12:02
 */
@Service
public class TeamInfoServiceImpl implements TeamInfoService {
    @Autowired
    private    TeamInfoMapper teamInfoMapper;

    @Autowired
    private TeamMasterMapper teamMasterMapper;

    public List<TeamInfo> getall(Integer page,Integer size) {
        return teamInfoMapper.getAll((page-1)*size,size);
    }

    //查询所有班级信息
    @Override
    public long getTotal() {
        return teamInfoMapper.getTotal();
    }


    //根据条件查询班级信息
    @Override
    public List<TeamInfo> getByCondition(TeamCondition condition, Integer page, Integer limit) {
        return teamInfoMapper.getByCondition(condition,(page-1)*limit,limit);
    }

    //根据条件查询记录总数
    @Override
    public long getCountByCondition(TeamCondition condition) {

        return teamInfoMapper.getCountByCondition(condition);
    }

    public TeamInfo getByTeid(TeamInfo teamInfo) {
        return teamInfoMapper.getByTeid(teamInfo);
    }

    public int delByTeid(TeamInfo teamInfo) {
        return teamInfoMapper.delByTeid(teamInfo);
    }

    /**
     * 根据条件更新
     * @param teamInfo
     * @return
     */
    @Transactional
    public int updateByTeid(TeamInfo teamInfo) {
        //查看是否更换班主任
        TeamInfo oldteam = teamInfoMapper.getByTeid(teamInfo);
        //如果更换了班主任
        if(teamInfo.getTmId()!=oldteam.getTmId()){
            //新建班主任信息
            TeamMaster teamMaster = new TeamMaster().setTcId(teamInfo.getTmId());

            //新增班主任信息
            int num = teamMasterMapper.addTeamMasert(teamMaster);
            //判断是否新增班主任信息成功
            if (num==1){
                //设置班主任id到班级信息
                teamInfo.setTmId(teamMaster.getTmId());
                num=teamInfoMapper.updateByTeid(teamInfo);
                //判断是否插入成功
                if (num==1){
                    return 1;
                }else {
                    //如果更新信息失败抛出异常
                    throw new  RuntimeException("更新班级信息失败");
                }
            }else {
                //如果插入信息失败抛出异常
                throw new  RuntimeException("新增班主任信息失败");
            }
        }
        //
        return teamInfoMapper.updateByTeid(teamInfo);
    }

    //新增班级
    public int add(TeamInfo teamInfo) {

        //新建班主任信息
        TeamMaster teamMaster = new TeamMaster().setTcId(teamInfo.getTmId());

        //新增班主任信息
        int num = teamMasterMapper.addTeamMasert(teamMaster);

        if (num==1){
            //设置班主任id到班级信息
            teamInfo.setTmId(teamMaster.getTmId());
            //信息新增到班级表
            num = teamInfoMapper
                    .add(teamInfo);
        }

        return num;
    }

    /**
     * 批量删除
     * @param array 删除的班级id数组
     * @return
     */
    @Override
    public int delBatch(String[] array) {
        return teamInfoMapper.delBatch(array);
    }
}
